<?php echo $header; ?>
<div id="content">
    <div class="breadcrumb">
        <?php foreach ($breadcrumbs as $breadcrumb) { ?>
        <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
        <?php } ?>
    </div>
    <?php if ($error_warning) { ?>
    <div class="warning"><?php echo $error_warning; ?></div>
    <?php } ?>
    <?php if ($error_probability_sum) { ?>
    <div class="warning"><?php echo $error_probability_sum; ?></div>
    <?php } ?>
    <?php if ($success) { ?>
    <div class="success"><?php echo $success; ?></div>
    <?php } ?>
    <div class="box">
        <div class="heading">
            <h1><img src="view/image/category.png" alt="" /> <?php echo $heading_title; ?></h1>
            <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
        </div>
        <div class="content">
            <form action="" method="post" id="form">
            <table class="list">
                <thead>
                <tr>
                    <td class="left"><?php echo $entry_prize; ?></td>
                    <td class="left"><?php echo $entry_item; ?></td>
                    <td class="left"><?php echo $entry_probability; ?></td>
                </tr>
                </thead>
                <tbody>
                <?php foreach($lottery as $key => $value){ ?>
                <tr>
                    <td class="left"><input type="text" readonly="readonly" name="lottery[<?php echo $key; ?>][id]" value="<?php echo $value['id']; ?>" /></td>
                    <td class="left">
                        <input type="text" <?php echo $key==8 ? 'readonly="readonly"' : ''; ?> name="lottery[<?php echo $key; ?>][item]" value="<?php echo $value['item']; ?>" size="100" /><?php echo $key>=3 && $key<8 ? ($key<6 ? '&nbsp;&nbsp;' . $text_coupon : '&nbsp;&nbsp;' . $text_reward_point) : ''; ?>
                        <?php
                         if($key<3){
                             $prize=$value['prize'];
                         }elseif($key>=3 && $key<6){
                             $prize=0;
                         }elseif($key>=6 && $key<8){
                             $prize=-1;
                         }else{
                             $prize=-2;
                         }
                        ?>
                        <input type="hidden" name="lottery[<?php echo $key; ?>][prize]" value="<?php echo $prize; ?>" />
                        <?php if(isset($error_item[$key])){ ?>
                        <span class="error"><?php echo $error_item[$key]; ?></span>
                        <?php } ?>
                        <?php if(isset($error_prize[$key])){ ?>
                        <span class="error"><?php echo $error_prize[$key]; ?></span>
                        <?php } ?>
                    </td>
                    <td class="left">
                        <input type="text" name="lottery[<?php echo $key; ?>][v]" value="<?php echo $value['v']; ?>" size="20" />
                        <?php if(isset($error_probability[$key])){ ?>
                        <span class="error"><?php echo $error_probability[$key]; ?></span>
                        <?php } ?>
                    </td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript"><!--
    $.widget('custom.catcomplete', $.ui.autocomplete, {
        _renderMenu: function(ul, items) {
            var self = this, currentCategory = '';

            $.each(items, function(index, item) {
                self._renderItem(ul, item);
            });
        }
    });
<?php for($i=0;$i<3;$i++){ ?>
$('input[name=\'lottery[<?php echo $i; ?>][item]\']').autocomplete({
    delay: 0,
    source: function(request, response) {
        $.ajax({
            url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
            dataType: 'json',
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item.name,
                        value: item.product_id
                    }
                }));
            }
        });
    },
    select: function(event, ui) {
        $('input[name=\'lottery[<?php echo $i; ?>][item]\']').val(ui.item.label);
        $('input[name=\'lottery[<?php echo $i; ?>][prize]\']').val(ui.item.value);

        return false;
    }
});
<?php } ?>
//--></script>
<?php echo $footer; ?>